package cn.green.config;

import cn.green.app.OrmAppContext;
import cn.green.core.adapt.models.field.abs.AbsOrmField;
import cn.green.core.adapt.models.table.OrmTable;
import cn.green.core.components.database.DBRouter;
import cn.green.core.components.ddao.QDdl;
import cn.green.core.model.QConditionModel;
import cn.green.core.model.QFieldModel;
import cn.green.core.model.QTableModel;
import cn.green.manage.cache.QCache;
import cn.green.manage.curd.QConditionCurd;
import cn.green.manage.curd.QFieldCurd;
import cn.green.manage.curd.QTableCurd;
import cn.green.manage.service.QDdlService;
import cn.green.mng.curd.QOrmConditionCurd;
import cn.green.mng.curd.QOrmFieldCurd;
import cn.green.mng.curd.QOrmTableCurd;
import cn.green.mng.init.OrmTableInitializer;
import cn.green.mng.sql.QOrmDdl;
import cn.green.utils.OrmAppUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisOperations;

import java.util.Collections;
import java.util.List;

/**
 * @author wang.yishu
 * @version 1.0
 * @className QOrmAutoConfiguration
 * @description 持久化组件的自动配置
 * @date 2025/4/30 16:49
 */
@Configuration

@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
public class QOrmAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean(QConditionCurd.class)
    public QConditionCurd qConditionCurd() {
        return new QOrmConditionCurd() {
            @Override
            public QConditionModel getConditionModel(String source, String scheme, String tableName) {
                return null;
            }

            @Override
            public void setConditionModel(QConditionModel model) {

            }

            @Override
            public void delConditionModel(String source, String scheme, String tableName) {

            }
        };
    }

    @Bean
    @ConditionalOnMissingBean(QFieldCurd.class)
    public QFieldCurd qFieldCurd() {
        System.err.println("QOrm Auto Configuration -> You have to configure QFieldCurd.");
        return new QOrmFieldCurd() {

            @Override
            public List<QFieldModel> findFieldsByTableName(String source, String scheme, String tableName) {
                return Collections.emptyList();
            }

            @Override
            public QFieldModel selectByTableNameAndFieldName(String source, String scheme, String tableName, String fieldName) {
                return null;
            }

            @Override
            public QFieldModel deleteFieldInfo(QFieldModel qFieldModel) {
                return null;
            }

            @Override
            public QFieldModel insertQFieldModel(QFieldModel model) {
                return null;
            }

            @Override
            public QFieldModel updateQFieldModel(QFieldModel model) {
                return null;
            }

            @Override
            public AbsOrmField insertOrmField(AbsOrmField ormField) {
                return null;
            }

            @Override
            public AbsOrmField updateOrmField(AbsOrmField ormField) {
                return null;
            }

            @Override
            public List<QFieldModel> findOrmFields(String source, String scheme, String table) {
                return Collections.emptyList();
            }

            @Override
            public List<QFieldModel> findNotOrmFields(String source, String scheme, String table) {
                return Collections.emptyList();
            }

            @Override
            public QFieldModel getField(String source, String scheme, String table, String column) {
                return null;
            }


        };
    }

    @Bean
    @ConditionalOnMissingBean(QTableCurd.class)
    public QTableCurd qTableCurd() {
        System.err.println("QOrm Auto Configuration -> You have to configure QTableCurd.");
        return new QOrmTableCurd() {
            @Override
            public List<OrmTable> ormTables() {
                return Collections.emptyList();
            }

            @Override
            public OrmTable updVersion(OrmTable ormTable) {
                return null;
            }

            @Override
            public List<QTableModel> findTable(QTableModel table) {
                return Collections.emptyList();
            }

            @Override
            public QTableModel insertTable(QTableModel table) {
                return null;
            }

            @Override
            public QTableModel selectTable(String source, String scheme, String tableName) {
                return null;
            }

            @Override
            public QTableModel updateTable(QTableModel table) {
                return null;
            }

            @Override
            public void deleteTable(String source, String scheme, String tableName) {
            }
        };
    }


    @Bean
    @ConditionalOnClass({DBRouter.class, QCache.class, QDdlService.class, QFieldCurd.class, QTableCurd.class, QDdl.class})
    public OrmTableInitializer ormTableInitializer(DBRouter dbRouter,
                                                   QCache cache,
                                                   QFieldCurd fieldCurd,
                                                   QTableCurd tableCurd,
                                                   QDdl ormDdl,
                                                   QDdlService qDdlService) {
        if (!(fieldCurd instanceof QOrmFieldCurd)) {
            throw new RuntimeException("fieldCurd must be instance of QOrmFieldCurd");
        }
        if (!(tableCurd instanceof QOrmTableCurd)) {
            throw new RuntimeException("tableCurd must be instance of QOrmTableCurd");
        }
        if (!(ormDdl instanceof QOrmDdl)) {
            throw new RuntimeException("ormDdl must be instance of QOrmDdl");
        }
        OrmTableInitializer ormTableInitializer = new OrmTableInitializer(dbRouter, (QOrmTableCurd) tableCurd, (QOrmFieldCurd) fieldCurd, cache,
                (QOrmDdl) ormDdl, qDdlService);
        ormTableInitializer.initOrmTable();
        return ormTableInitializer;
    }


    @Bean
    @ConditionalOnBean(OrmAppContext.class)
    public OrmAppUtils OrmAppUtils (OrmAppContext context) {
        OrmAppUtils ormAppUtils = new OrmAppUtils();
        ormAppUtils.setApplicationContext(context.getApplicationContext());
        return ormAppUtils;
    }

    //================================================== 以下均为测试使用 ===================================================
}
